#include "hw_motor_class.h"

#include <thread>
#include <chrono>
#include <stdlib.h>

using namespace std;

HW_Motor_Class* HW_Motor_Class::handler = nullptr;
static int motorspeed = 0;

HW_Motor_Class::HW_Motor_Class()
{
    threadstatus = 1;
}

void HW_Motor_Class::set_motor_speed(int speed)
{
    motorspeed = speed;
}

void HW_Motor_Class::stopmotor()
{
    threadstatus = -1;
}

void HW_Motor_Class::running()
{
    system("echo 144 > /sys/class/gpio/export");
    system("echo out > /sys/class/gpio/gpio144/direction");
    while (threadstatus > 0) {
        if(motorspeed > 0) // 6 - 14
        {
            system("echo 1 > /sys/class/gpio/gpio144/value");
            std::this_thread::sleep_for(std::chrono::milliseconds(31-motorspeed));
            system("echo 0 > /sys/class/gpio/gpio144/value");
            std::this_thread::sleep_for(std::chrono::milliseconds(30-motorspeed));
        }
        else
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(20));
        }
    }
}
